from flask import Blueprint, jsonify, request  # 导入蓝图
from flask_restful import Api, Resource
from models.models import *
import math

# url_prefix   指定当前蓝图下所有路由的前缀
user_db = Blueprint('user_db',__name__,url_prefix='/users')  # 实例化蓝图对象

api = Api(user_db)    # 实例化蓝图收集器  #实例化一个Api对象的作用是来搜集路由的

# 定义类视图  继承自Resource类，通过Api的实例化对象进行搜集

# class UserView(Resource):
#     def get(self):
@user_db.route('/get')
def post():
        # 获取参数page
        page = int(request.args.get('page'))
        total = BjfyModel.query.count()
        # 定义每页显示多少条
        page_size = 1
        # 查询当前页记录
        start = math.ceil((page - 1) * page_size)
        fy = BjfyModel.query.offset(start).limit(page_size).all()
        # fy = BjfyModel.query.all()
        print('=========', fy)
        list = [{
            "id": i.id,
            "name": i.name,
            "direction": i.direction
        } for i in fy]
        # return jsonify({'code':200,"flist":list})
        return jsonify({"code": 200, "total": total, "page_size": page_size, "flist": list})


# 添加路由
# api.add_resource(UserView,'/user')

